package com.bbm.communitysystem.config;

import com.bbm.communitysystem.domain.Comment;
import com.bbm.communitysystem.domain.Per;
import com.bbm.communitysystem.domain.Post;
import com.bbm.communitysystem.domain.Role;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean(name = "main")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }


    @Bean(name = "per")
    public RedisTemplate<String, Per> redisTemplatePer(RedisConnectionFactory factory) {

        RedisTemplate<String,Per> redisTemplate = new RedisTemplate<>();

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Per>(Per.class));

        redisTemplate.setConnectionFactory(factory);

        return redisTemplate;
    }


    @Bean(name = "role")
    public RedisTemplate<String, Role> redisTemplateRole(RedisConnectionFactory factory) {

        RedisTemplate<String,Role> redisTemplate = new RedisTemplate<>();

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Role>(Role.class));

        redisTemplate.setConnectionFactory(factory);

        return redisTemplate;
    }

    @Bean(name = "post")
    public RedisTemplate<String, Post> redisTemplatePost(RedisConnectionFactory factory) {

        RedisTemplate<String,Post> redisTemplate = new RedisTemplate<>();

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Post>(Post.class));

        redisTemplate.setConnectionFactory(factory);

        return redisTemplate;
    }

    @Bean(name = "comment")
    public RedisTemplate<String, Comment> redisTemplateComment(RedisConnectionFactory factory) {

        RedisTemplate<String,Comment> redisTemplate = new RedisTemplate<>();

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Comment>(Comment.class));

        redisTemplate.setConnectionFactory(factory);

        return redisTemplate;
    }



}
